package com.naire.modul.questionnaire.service;

import java.util.ArrayList;
import java.util.List;

import net.common.service.GeneralService;
import net.common.util.FilePathUtils;
import net.common.util.XMLHandler;

import org.dom4j.Element;

import com.naire.modul.projeckpi.dao.ProjeckpiDao;
import com.naire.modul.projeckpi.dao.impl.ProjeckpiDaoImpl;
import com.naire.modul.projeckpi.vo.ProjeckpiVo;
import com.naire.modul.questionnaire.dao.Projeckpi_answerDao;
import com.naire.modul.questionnaire.dao.impl.Projeckpi_answerDaoImpl;
import com.naire.modul.questionnaire.vo.Projeckpi_answerVo;
import com.naire.modul.questionnaire.vo.ProjecoptionVo;

public class QuestionnaireCreateService extends GeneralService {

	private Integer project_id;
	private String html;
	private Projeckpi_answerDao answerDao;
	private String doc;

	@Override
	public void execute() throws Exception {
		ProjeckpiDao dao = new ProjeckpiDaoImpl();
		answerDao = new Projeckpi_answerDaoImpl();
		List<ProjeckpiVo> projectKpies = dao.searchAllKpi(project_id); // 项目指标
		List<ProjeckpiVo> newKpies = new ArrayList<ProjeckpiVo>();
		for (ProjeckpiVo vo : projectKpies) {
			if (vo.getParent_num() == null) {
				newKpies.add(vo);
				handlerProjectKpi(projectKpies, vo.getVoes(), vo.getKpi_num());
			}
		}
		handlerProjectKpi(projectKpies, newKpies, null);

		XMLHandler xml = new XMLHandler(); // XML
		Element root = xml.createRoot("TEMPLATE"); // 模板根元素
		createXML(newKpies, root);
		html = xml.xmlTransform(FilePathUtils.getTemplateXml());
		doc = xml.toXml();
	}
	
	/**
	 * 指标树处理
	 * 
	 * @param source
	 * @param target
	 * @param parent_code
	 */
	private void handlerProjectKpi(List<ProjeckpiVo> source,
			List<ProjeckpiVo> target, String parent_code) {
		for (ProjeckpiVo vo : source) {
			if (parent_code != null && parent_code.equals(vo.getParent_num())) {
				target.add(vo);
				handlerProjectKpi(source, vo.getVoes(), vo.getKpi_num());
			}
		}
	}

	/**
	 * 创建生成XML
	 * 
	 * @param newKpies
	 * @throws Exception
	 */
	private void createXML(List<ProjeckpiVo> newKpies, Element element)
			throws Exception {
		for (ProjeckpiVo vo : newKpies) {
			Element e = createKpiElement(element, vo); // KPI元素
			if (vo.getVoes() != null && vo.getVoes().size() > 0) {
				// 加入指标
				createXML(vo.getVoes(), e);
			} else {
				// 加入指标问题
				List<Projeckpi_answerVo> list = answerDao.search(project_id, vo.getKpi_num());
				createEntryElement(e, list);
			}
		}
	}
	
	/**
	 * 创建样题
	 * 
	 * @param element
	 * @param list
	 */
	private void createEntryElement(Element element, List<Projeckpi_answerVo> list) {
		for(Projeckpi_answerVo vo : list) {			
			Element entry = element.addElement("ENTRY");
			entry.addElement("ID").addCDATA(String.valueOf(vo.getProject_answer_id())); // 指标唯一标识
			entry.addElement("CONTENT").addCDATA(vo.getTitle()); // 标题
			entry.addElement("ANSWER-TYPE").addCDATA(vo.getAnswer_type()); // 题类型
			entry.addElement("ANSWER-RULE").addCDATA(vo.getAnswer_rule()); // 题得分规则
			entry.addElement("SOURCE").addCDATA(String.valueOf(vo.getSource().doubleValue())); // 题分数
			entry.addElement("STANDARD-SOURCE").addCDATA("0"); // 标准得分
			entry.addElement("SCORING").addCDATA("0"); // 有效得分
			entry.addElement("ANSWER-DESC").addCDATA(vo.getAnswer_desc()); // 描述
			entry.addElement("ANSWER-REMARK").addCDATA(vo.getAnswer_remark()); // 备注
			entry.addElement("USE").addCDATA(""); // 备注
			for(ProjecoptionVo po : vo.getOptions()) {
				Element item = entry.addElement("ITEM");
				item.addElement("CONTENT").addCDATA(po.getOption_title());
				item.addElement("OPTION-TYPE").addCDATA(po.getOption_type());
				item.addElement("OPTION-DESC").addCDATA(po.getOption_desc());
				item.addElement("OPTION-REMARK").addCDATA(po.getOption_remark());
				item.addElement("SOURCE").addCDATA(String.valueOf(po.getSource().doubleValue()));
				item.addElement("CHECKED").addCDATA("");
				item.addElement("INPUT-DESC").addCDATA("");
			}
		}
	}

	/**
	 * 创建KPI元素
	 * 
	 * @param element
	 * @param vo
	 * @return
	 * @throws Exception
	 */
	private Element createKpiElement(Element element, ProjeckpiVo vo)
			throws Exception {
		Element kpi = element.addElement("KPI");
		kpi.addElement("ID").addCDATA(vo.getKpi_num()); // 指标唯一标识
		kpi.addElement("CONTENT").addCDATA(vo.getKpi_name()); // 指标描述
		kpi.addElement("SOURCE").addCDATA(
				String.valueOf(vo.getSource().doubleValue())); // 指标分数
		kpi.addElement("STANDARD-SOURCE").addCDATA("0");
		kpi.addElement("SCORING").addCDATA("0");
		return kpi;
	}

	public String getHtml() {
		return html;
	}

	public String getDoc() {
		return doc;
	}

	public void setProject_id(Integer project_id) {
		this.project_id = project_id;
	}

}
